<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>combine</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const combine = (a, b, prop) =>
            Object.values(
                [...a, ...b].reduce((acc, v) => {
                    const item = v[prop];
                    if (item) {
                        acc[item] = acc[item] ? { ...acc[item], ...v } : { ...v };
                    }
                    return acc;
                }, {})
            );
        const x = [
            { id: 1, name: 'John' },
            { id: 2, name: 'Maria' }
        ];
        const y = [{ id: 1, age: 28 }, { id: 3, age: 26 }, { age: 3 }];
        console.log(combine(x, y, 'id'));;
    </script>
</body>

</html>